﻿
Partial Class WebForm_ManageGroup_GroupControl
    Inherits System.Web.UI.UserControl


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.Page.IsPostBack Then
            Me.BindDDLDivision()
            Me.BinDDLDimension()
            Me.BindGV()
        End If
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub
    Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing
        Me.gv.EditIndex = e.NewEditIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gv.RowCancelingEdit
        Me.gv.EditIndex = -1
        Me.BindGV()
    End Sub

    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        Dim key As DataKey = gv.DataKeys(e.RowIndex)
        Dim txtgvGroupName As TextBox = TryCast(gv.Rows(e.RowIndex).FindControl("txtgvGroupName"), TextBox)
        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("Update GroupControl ")
            sql.AppendFormat("set GroupName = '{0}' ", txtgvGroupName.Text.Trim().Replace("'", "''"))
            sql.AppendFormat(",UpdateDate=getdate() ")
            sql.AppendFormat(",UpdateBy='{0}' ", clsAuth.GetUserID())
            sql.AppendFormat("where GroupID = '{0}' ", key("GroupID"))
            dat.AccData(sql)

            scope.Complete()
        End Using
        gv.EditIndex = -1
        Me.BindGV()
    End Sub
    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        Dim key As DataKey = gv.DataKeys(e.RowIndex)

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("delete from GroupControl ")
            sql.AppendFormat("where GroupID = '{0}' ", key("GroupID"))
            sql.AppendFormat(";delete from GroupControlSet ")
            sql.AppendFormat("where GroupID = '{0}' ", key("GroupID"))
            dat.AccData(sql)
            scope.Complete()
        End Using
        Me.BindGV()
    End Sub
    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("Insert into GroupControl(Division,Dimension,GroupName,CreateBy,CreateDate) ")
            sql.AppendFormat("select '{0}' ", Me.ddlDivision.SelectedValue)
            sql.AppendFormat(",'{0}' ", Me.ddlDimension.SelectedValue)
            sql.AppendFormat(",'{0}' ", Me.txtGroupName.Text.Trim().Replace("'", "''"))
            sql.AppendFormat(",'{0}' ", clsAuth.GetUserID())
            sql.AppendFormat(",{0} ", "getdate()")
            dat.AccData(sql)
            scope.Complete()
        End Using
        Me.txtGroupName.Text = ""
        Me.BindGV()
    End Sub
    Protected Sub ddlDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDivision.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub ddlDimension_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDimension.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
        Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
        Dim Dimension As String = ""
        Dim GroupID As String = ""
        Dim Division As String = ""
        If e.CommandName = "Set" Then
            Dimension = Me.gv.DataKeys(row.RowIndex).Item("Dimension").ToString()
            GroupID = Me.gv.DataKeys(row.RowIndex).Item("GroupID").ToString()
            Division = Me.gv.DataKeys(row.RowIndex).Item("Division").ToString()
            Dim open As String = "window.open('" + Me.ResolveUrl(String.Format("~/Webform/ManageGroup/OpenGroup.aspx?GroupID={0}&Division={1}&Dimension={2}", GroupID, Division, Dimension)) + "','newwindow','height=575px,toolbar=0,titlebar=0,resizable,scrollbars=Yes');"
            ScriptManager.RegisterClientScriptBlock(TryCast(HttpContext.Current.Handler, Page), GetType(String), "onclick", open.ToString, True)
        End If
    End Sub
    Private Sub BindDDLDivision()
        Dim ddlControl As DropDownList = Me.ddlDivision
        Dim dvManager As New DivisionManager

        ddlControl.DataTextField = "Division"
        ddlControl.DataValueField = "Division"
        ddlControl.DataSource = dvManager.GetAllDivision()
        ddlControl.DataBind()
        ddlControl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub
    Private Sub BinDDLDimension()
        Dim ddlControl As DropDownList = Me.ddlDimension

        ddlControl.DataTextField = "Text"
        ddlControl.DataValueField = "Value"
        ddlControl.DataSource = MyObj.GetCodeMappingData("Product", "Dimension")
        ddlControl.DataBind()
        ddlControl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub
    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" SELECT g.Division,g.GroupName,g.Dimension,g.GroupID,c.Text as DimensionText FROM GroupControl g ")
        sql.AppendFormat("INNER JOIN CodeMapping c on g.Dimension=c.Value ")
        sql.AppendFormat("AND c.TableName='Product' AND c.ColumnName='Dimension' ")
        sql.AppendFormat(" WHERE 1=1 ")

        If Me.ddlDivision.SelectedValue <> "" Then
            sql.AppendFormat(" AND  g.Division = '{0}' ", Me.ddlDivision.SelectedValue)
        End If

        If Me.ddlDimension.SelectedValue <> "" Then
            sql.AppendFormat("AND  g.Dimension = '{0}' ", Me.ddlDimension.SelectedValue)
        End If

        sql.AppendFormat(" ORDER BY g.Division ")

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()

    End Sub

 

   
End Class
